package com.wyg.blog.controller.manage_blog;

import com.alibaba.fastjson.JSON;
import com.wyg.blog.model.user.AuthorInformation;
import com.wyg.blog.service.user.AuthorInfoService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2021/1/30 11:34
 */
@RestController
@RequestMapping("/api/authorInfo")
public class AuthInfoController {
    @Resource
    AuthorInfoService authorInfoService;

    @PostMapping("/updateInfo")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')")
    public JsonResult<Boolean> updateInfo(@RequestBody Map<String, String> authorInfo) {
        AuthorInformation authorInformation = new AuthorInformation();
        authorInformation.setId(Integer.parseInt(authorInfo.get("id")));
        authorInformation.setAvatar(authorInfo.get("avatar"));
        authorInformation.setNickname(authorInfo.get("nickname"));
        authorInformation.setMotto(authorInfo.get("motto"));
        authorInformation.setContent(authorInfo.get("content"));
        authorInformation.setText_content(authorInfo.get("text_content"));
        authorInformation.setBackground(authorInfo.get("background"));
        boolean res = authorInfoService.renewInfo(authorInformation);
        if (res) {
            return new JsonResult<>(true, "200", "更新成功!");
        } else {
            return new JsonResult<>(false, "1001", "更新失败!");
        }
    }

    @GetMapping("/getInfo")
    @CrossOrigin
    @PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')")
    public JsonResult<Map<String, Object>> getAuthorInfo(@RequestParam("id") Integer id) {
        Map<String, Object> res = new HashMap<>();
        AuthorInformation authorInformation = authorInfoService.selectInfo(id);
        String motto = authorInformation.getMotto();
        String content = authorInformation.getContent();
        String nickname = authorInformation.getNickname();
        String text_content = authorInformation.getText_content();
        res.put("avatar", authorInformation.getAvatar());
        res.put("nickname", nickname);
        res.put("motto", motto);
        res.put("content", content);
        res.put("text_content", text_content);
        res.put("background", authorInformation.getBackground());
        return new JsonResult<>(res, "200","获取个人简介成功!");
    }
}
